// Author: wsfuyibing <682805@qq.com>
// Date: 2025-05-21

package category

import (
	"gitee.com/go-libs/db-xorm/db"
	"server/app/errs"
	"server/app/http/models"
	"server/app/kind"
)

type Logic struct{}

func (o *Logic) CalcForSale(sess *db.Session, aid kind.ArcadeId, cid int64) (count int64, err error) {
	if count, err = sess.Where("arcade_id = ? AND category_id = ? AND state = ?",
		aid, cid, kind.GoodsStateForSale,
	).Count(models.Goods{}); err != nil {
		err = errs.ErrDatabaseQuery
	}
	return
}

func (o *Logic) CalcTotal(sess *db.Session, aid kind.ArcadeId, cid int64) (count int64, err error) {
	if count, err = sess.Where("arcade_id = ? AND category_id = ?",
		aid, cid,
	).Count(models.Goods{}); err != nil {
		err = errs.ErrDatabaseQuery
	}
	return
}

func (o *Logic) CalcUpdate(sess *db.Session, aid kind.ArcadeId, total, enables int64) (err error) {
	if _, err = sess.Cols("goods_total", "goods_count").Where("id = ?",
		aid,
	).Update(&models.GoodsCategory{
		GoodsTotal: int(total),
		GoodsCount: int(enables),
	}); err != nil {
		err = errs.ErrDatabaseQuery
	}
	return
}
